 package com.p2p.cms.controller.admin;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.p2p.base.annotation.PermissionNeed;
import com.p2p.base.page.PageVo;
import com.p2p.base.web.controller.BaseController;
import com.p2p.cms.service.CategoryService;
import com.p2p.cms.service.PostService;



/**
 * 
 * @author fansunion@qq.com 2014年11月2日
 */
@Controller
@RequestMapping("/admin/post")
public class AdminPostController extends BaseController {
	private static final String CATEGORY_ROWS = "categoryRows";

	@Autowired
	private PostService postService;

	@Autowired
	private CategoryService categoryService;

	// 获得一页内容
	@RequestMapping(value = "list")
	public String list(PageVo page, Model model) {
		PageVo vo = postService.pageAdmin(page);
		model.addAttribute("page", vo);
		prepareCategoryRows(vo);
		return "admin/post/index";
	}

	private void prepareCategoryRows(PageVo vo) {
		List<Map<String, Object>> categoryRows = categoryService.listAll();
		vo.getOtherData().put(CATEGORY_ROWS, categoryRows);
	}
	
	// 查看post，id为post的id
	@RequestMapping(value = "/detail/{id}")
	public String view(@PathVariable("id") Integer id, Model model) {
		Map<String, Object> post = postService.get(id);
		model.addAttribute("post", post);
		prepareCategoryRowsForView(model, post);
		return "admin/post/view";
	}

	private void prepareCategoryRowsForView(Model model,
			Map<String, Object> params) {
		List<Map<String, Object>> categoryRows = categoryService.listAll();
		model.addAttribute(CATEGORY_ROWS, categoryRows);
	}

	@RequestMapping("add")
	public String add(Model model) {
		categoryRows(model);
		return "admin/post/add";
	}

	private void categoryRows(Model model) {
		List<Map<String,Object>> categoryList=categoryService.listAllChild();
		model.addAttribute("categoryList", categoryList);
	}
	
	@RequestMapping("update")
	public String edit(@RequestParam Integer id, Model model) {
		categoryRows(model);
		Map<String, Object> post = postService.get(id);
		model.addAttribute("post", post);
		return "admin/post/edit";
	}

	// 更新一条
	@RequestMapping(value = "/doupdate")
	public  void doUpdate(@RequestParam Map<String, Object> params,
			HttpServletRequest request, Model model) {
		buildSummary(params);
		postService.update(params);
		model.addAttribute("code", 1);
	}

	/***************** 更新 ******************/
	// 增加一条
	@RequestMapping(value = "/doadd")
	public  void doAdd(@RequestParam Map<String, Object> params,
			HttpServletRequest request, Model model) {
		buildSummary(params);
		postService.add(params);
		model.addAttribute("code", 1);
		model.addAttribute("id", params.get("id"));
	}

	// 逻辑删除一条
	@RequestMapping(value = "/doremove")
	public void doRemove(@RequestParam Integer id, HttpServletRequest request,
			Model model) {
		postService.remove(id);
		model.addAttribute("code", 1);
	}

	// 如果没有设置摘要，构造摘要
	private void buildSummary(Map<String, Object> params) {
		Object contentTxt = params.get("contentTxt");
		if (contentTxt == null) {
			return;
		}

		if (params.get("summary") != null) {
			return;
		}
		String txt = contentTxt.toString();
		if (txt.length() > 500) {
			txt = txt.substring(0, 500);
		}
		params.put("summary", txt);

	}
}
